﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Abp.Modules;
using Abp.Reflection.Extensions;
using AbpBuild.Configuration;
using AbpBuild;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Castle.Windsor.MsDependencyInjection;
using AbpBuild.EntityFrameworkCore;
using Abp.AspNetCore;
using Abp.AspNetCore.SignalR;
using Abp.AspNetCore.Configuration;
using System.Reflection;
using Abp.Events.Bus;
using Castle.MicroKernel.Registration;
using Microsoft.Extensions.FileProviders;
using System;
using Microsoft.Extensions.PlatformAbstractions;

namespace WenShuConsoleApp
{
    [DependsOn(
         typeof(AbpBuildApplicationModule)
     )]
    [DependsOn(typeof(AbpBuildEntityFrameworkModule))]
    public class CompanyConsoleAppModule : AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;
        //private readonly IHostingEnvironment _env;

        public CompanyConsoleAppModule(AbpBuildEntityFrameworkModule abpProjectNameEntityFrameworkModule)
        {
            abpProjectNameEntityFrameworkModule.SkipDbSeed = true;
            _appConfiguration = AppConfigurations.Get(
                typeof(CompanyConsoleAppModule).GetAssembly().GetDirectoryPathOrNull()
            );
            //_env = env;
        }

        public override void PreInitialize()
        {
            Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(
                AbpBuildConsts.WenShuDbConnectionStringName
            );
            
            Configuration.BackgroundJobs.IsJobExecutionEnabled = false;
            Configuration.ReplaceService(
                typeof(IEventBus),
                () => IocManager.IocContainer.Register(
                    Component.For<IEventBus>().Instance(NullEventBus.Instance)
                )
            );
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(CompanyConsoleAppModule).GetAssembly());
        }
    }


    public class CustomHostingEnvironment : Microsoft.AspNetCore.Hosting.IHostingEnvironment, Abp.Dependency.ISingletonDependency
    {
        public CustomHostingEnvironment()
        {
            
            this.ContentRootPath = Environment.CurrentDirectory;
            this.EnvironmentName = "Development";

            //Assembly assembly = typeof(Program).GetTypeInfo().Assembly;
            //AssemblyName assemblyName = assembly.GetName();
            //ApplicationEnvironment env = PlatformServices.Default.Application;

        }
        public string ApplicationName { get; set; }
        public IFileProvider ContentRootFileProvider { get; set; }
        public string ContentRootPath { get; set; }
        public string EnvironmentName { get; set; }
        public Microsoft.Extensions.FileProviders.IFileProvider WebRootFileProvider { get; set; }
        public string WebRootPath { get; set; }
    }

}
